package com.zoe.onelink.business.resolver;

import com.zoe.onelink.util.OnelinkContextHolder;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver;

import java.util.Optional;

/**
 * <p>标题: 该解析器专门用来解决旧项目业务组说代码改造量太大,改不动的问题</p>
 * <p>描述: Controller 入参使用@RequestHeader注解如果取不到值时,尝试使用ThreadParameterUtil在获取一次</p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-03-17
 */
public class OnelinkRequestHeaderMethodArgumentResolver extends RequestHeaderMethodArgumentResolver {

    public OnelinkRequestHeaderMethodArgumentResolver(ConfigurableBeanFactory beanFactory) {
        super(beanFactory);
    }

    @Override
    protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
        return Optional.ofNullable(super.resolveName(name, parameter, request))
                .orElse(OnelinkContextHolder.getString(name));
    }
}
